#!python2
#-*- coding:utf-8 -*-

try:
    from Tkinter import *
except Exception as e:
    print e
    raw_input('press any key...')
    sys.exit()
from MyThread import MyThread
from Step_route import Step_route
import os

class Gui():
    def __init__(self,master):
        pwd=os.getcwd()
        self.url=os.path.join(pwd,'updown_nmon.conf')
        self.npath_var = StringVar()
        self.ippath_var = StringVar()
        self.respath_var = StringVar()
        self.ana_path_var = StringVar()

        #设置路径信息，如果updown_nmon.conf存在，读取信息，如果不存在，设置提示信息
        self.iplist_tip = r'路径+文件名，例：C:\Users\user\Desktop\iplist.xlsx'
        self.nmon_path_tip = r'路径+文件名，例：C:\Users\user\Desktop\nmon'
        self.result_path_tip = r'保存监控结果的文件夹，例:e:\test_result' + '\\'
        self.ana_path_tip = r'分析结果保存路径，若不指定文件名，则结果为ana_result.xlsx，例：e:\analysis_result\或e:\ana_result.xlsx'
        if os.path.exists(self.url):
            with open(self.url,'r') as f:
                self.ippath_var.set(f.readline().strip('\n'))
                self.respath_var.set(f.readline().strip('\n'))
                self.npath_var.set(f.readline().strip('\n'))
                self.ana_path_var.set(f.readline().strip('\n'))
        else:
            self.npath_var.set(self.nmon_path_tip)
            self.ippath_var.set(self.iplist_tip)
            self.respath_var.set(self.result_path_tip)
            self.ana_path_var.set(self.ana_path_tip)

        #界面框架
        self.frm_top1=Frame(master)
        self.frm_top2=Frame(master)
        self.frm1=Frame(self.frm_top1)
        self.frm2=Frame(self.frm_top1)
        self.frm3=Frame(self.frm_top2)

        #左侧按钮
        Button(self.frm1, text='启动nmon', bg='green',command=self.upnmon).pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(self.frm1, text='结束nmon进程',bg='red',command=self.killnmon).pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(self.frm1, text='下载nmon结果',command=self.downloadRes).pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(self.frm1, text='删除nmon结果',command=self.deleteRes).pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(self.frm1, text='上传nmon',command=self.uploadNmon).pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(self.frm1, text='分析结果文件',command=self.parseResult).pack(side=TOP, anchor=W, fill=X, expand=YES)

        #右侧路径标签、路径文本框、事件绑定
        Label(self.frm2, text='IPlist 路径').pack(side=TOP,anchor=W)
        self.iplist_path = Entry(self.frm2, textvariable=self.ippath_var)
        self.iplist_path.pack(side=TOP,fill=X, anchor=W)
        self.iplist_path.bind('<Button-1>', self.iplist_path_clear)
        self.iplist_path.bind('<FocusOut>', self.iplist_path_insert)

        Label(self.frm2, text='下载结果保存位置').pack(side=TOP, anchor=W)
        self.result_path = Entry(self.frm2, textvariable=self.respath_var)
        self.result_path.pack(side=TOP, fill=X, anchor=W)
        self.result_path.bind('<Button-1>', self.result_path_clear)
        self.result_path.bind('<FocusOut>', self.result_path_insert)

        Label(self.frm2,text='本地nmon位置').pack(side=TOP,anchor=W)
        self.nmonpath=Entry(self.frm2,textvariable=self.npath_var)
        self.nmonpath.pack(side=TOP, anchor=W, fill=X)
        self.nmonpath.bind('<Button-1>', self.nmonpath_clear)
        self.nmonpath.bind('<FocusOut>', self.nmonpath_insert)

        Label(self.frm2, text='分析结果保存位置').pack(side=TOP, anchor=W)
        self.analysis_path = Entry(self.frm2, textvariable=self.ana_path_var)
        self.analysis_path.pack(side=TOP, anchor=W, fill=X)
        self.analysis_path.bind('<Button-1>', self.analysis_path_clear)
        self.analysis_path.bind('<FocusOut>', self.analysis_path_insert)


        self.res=Text(self.frm3)
        self.res.pack(side=LEFT,fill=X,expand=YES)

        #框架布局
        self.frm1.pack(side=LEFT, fill=BOTH, expand=YES)
        self.frm2.pack(side=LEFT, fill=BOTH, expand=YES)
        self.frm3.pack(side=LEFT, fill=BOTH, expand=YES)
        self.frm_top1.pack(side=TOP,fill=BOTH,expand=YES)
        self.frm_top2.pack(side=TOP,fill=BOTH,expand=YES)


    #文本框事件，点击文本框时如果已有提示信息，则清除
    #鼠标离开时，没有填写信息，则重新显示提示信息
    def iplist_path_clear(self, *args):
        if self.ippath_var.get() == self.iplist_tip:
            self.ippath_var.set('')

    def iplist_path_insert(self, *args):
        if self.ippath_var.get() == '':
            self.ippath_var.set(self.iplist_tip)

    def result_path_clear(self, *args):
        if self.respath_var.get() == self.result_path_tip:
            self.respath_var.set('')

    def result_path_insert(self, *args):
        if self.respath_var.get() == '':
            self.respath_var.set(self.result_path_tip)

    def nmonpath_clear(self, *args):
        if self.npath_var.get() == self.nmon_path_tip:
            self.npath_var.set('')

    def nmonpath_insert(self, *args):
        if self.npath_var.get() == '':
            self.npath_var.set(self.nmon_path_tip)

    def analysis_path_clear(self, *args):
        if self.ana_path_var.get() == self.ana_path_tip:
            self.ana_path_var.set('')

    def analysis_path_insert(self, *args):
        if self.ana_path_var.get() == '':
            self.ana_path_var.set(self.ana_path_tip)
    #按钮事件
    def upnmon(self):
        step='upNmon'
        self.execute_command(step)
        
    def killnmon(self):
        step='killNmon'
        self.execute_command(step)

    def downloadRes(self):
        step='downloadResult'
        self.execute_command(step)

    def deleteRes(self):
        step = 'DeleteResult'
        self.execute_command(step)

    def uploadNmon(self):
        step = 'upload_nmon'
        self.execute_command(step)
        
    def parseResult(self):
        step='parseResult'
        self.execute_command(step)

    #检查路径是否符合要求
    def ippathcheck(self):
        a=self.ippath_var.get()
        b=a.split('.')[-1]
        if a==self.iplist_tip:
            self.set_res('请填写IPlist 路径！\n')
            return 0
        if not os.path.isfile(a):
            self.set_res('没有找到“%s”，请检查！\n' % (a))
            return 0
        if b!='xls' and b!='xlsx':
            self.set_res('“%s”不是excel文档，请检查！\n' % (a))
            return 0
        return 1

    def respathcheck(self):
        a = self.respath_var.get()
        if a == self.result_path_tip:
            self.set_res('请填写下载结果保存位置！\n')
            return 0
        if '.' in a:
            self.set_res('下载结果保存位置应为文件夹！\n')
            return 0
        return 1

    def npathcheck(self):
        a = self.npath_var.get()
        if a == self.nmon_path_tip:
            self.set_res('请填写本地nmon路径！\n')
            return 0
        if not os.path.isfile(a):
            self.set_res('%s不存在或不是一个文件！\n'%(a))
            return 0
        return 1
    def anapathcheck(self):
        a=self.ana_path_var.get()
        if a == self.ana_path_tip:
            self.set_res('请填写分析结果保存位置！\n')
            return 0
        return 1
    def upNmoncheck_path(self):
        return self.ippathcheck()
    def killNmoncheck_path(self):
        return self.ippathcheck()
    def downloadResultcheck_path(self):
        return self.ippathcheck() and self.respathcheck()
    def upload_nmoncheck_path(self):
        return self.ippathcheck() and self.npathcheck()
    def DeleteResultcheck_path(self):
        return self.ippathcheck()
    def parseResultcheck_path(self):
        return self.respathcheck() and self.anapathcheck()

    #按钮事件
    def execute_command(self,step):
        self.res.delete(0.0, END)
        self.res.update()
        if not eval('self.%scheck_path()' % (step)):
            return 0
        iplist = self.iplist_path.get()
        local_nmon_path=self.nmonpath.get()
        local_result_path=self.result_path.get()
        analysis_path=self.ana_path_var.get()
        self.savePath()
        result = MyThread(Step_route,(iplist,step,local_nmon_path,local_result_path,analysis_path,a),step)
        result.start()

    #结果文本框赋值
    def set_res(self,res):
        self.res.insert(END, res)
        self.res.update_idletasks()
    #每次执行时保存路径信息到updown_nmon.conf，方便下次启动时读取
    def savePath(self):
        iplist_path=self.ippath_var.get()
        local_nmon_path=self.npath_var.get()
        local_result_path=self.respath_var.get()
        analysis_path=self.ana_path_var.get()
        with open(self.url,'w') as f:
            f.write(iplist_path+'\n')
            f.write(local_result_path+'\n')
            f.write(local_nmon_path+'\n')
            f.write(analysis_path)
if __name__=='__main__':
    try:
        root = Tk()
        root.title('一键启停nmon')
        root.geometry('900x600')
        a=Gui(root)
        root.mainloop()
    except Exception as e:
        sys.stdout.write(e)
